package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.MoveOrderAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.mvp.presenter.AppraiserPresenter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.AppraiserPresenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.AppraiserView;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.orhanobut.logger.Logger;

import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.AppraiserBean;
import rxretrofit_base.model.KeyBean;

/**
 * Created by yidong on 2017/5/31.
 */
public class MoveOrderActivity extends BaseActivity implements MultiItemTypeAdapter.OnItemClickListener,AppraiserView,MoveOrderAdapter.OnContentFillListener{
    @InjectView(R.id.recycler_appres)
    RecyclerView recyclerView;
    @InjectView(R.id.tv_title)
    TextView titleTv;
    private MoveOrderAdapter adapter;
    private List<AppraiserBean> appraiserList;
    private String dispatchId;
    private String appraId;
    private String content;
    private AppraiserPresenter presenter;
    private EditText contentEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move_order);
        ButterKnife.inject(this);
        presenter = new AppraiserPresenterImpl(this);
        presenter.getAllAppraiser();
        titleTv.setText("转单");

    }

    private void initViews() {
        adapter = new MoveOrderAdapter(this, R.layout.layout_item_move_orders, appraiserList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        adapter.setOnContentFillListener(this);
    }

    @Override
    public void getAllAppraiser(List<AppraiserBean> listbean) {
        appraiserList = listbean;
        initViews();
    }

    @Override
    public void moveOrdersTrack(String massage) {
            showToast(massage);
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {


        for (int i = 0; i < appraiserList.size(); i++) {
            if (i == position) {
                appraiserList.get(i).setSelected(true);
            } else {
                appraiserList.get(i).setSelected(false);
            }
        }
        appraId = appraiserList.get(position).getId();
        adapter.notifyDataSetChanged();
    }
    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Override
    public void onContentFill(int position, String price) {
        Logger.d(position +  "======" +price);
        content = price;
    }

    @OnClick({R.id.iv_return, R.id.btn_determine})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:
                finish();
                break;
            case R.id.btn_determine:
                Logger.d(content +  "======" +appraId);
                dispatchId = CustomSharedPreferences.getString(this, KeyBean.ORDER_ID.valueOf(),KeyBean.FILE_NAME.valueOf());
                presenter.moveOrdersTrack(dispatchId,appraId,content);
                break;
        }
    }


}
